package com.xxl.job.executor.helper;

import com.alibaba.fastjson.JSONObject;
import com.xxl.job.executor.util.HttpUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CaiHongPiHelper {

    @Getter
    public static String caiHongPiKey;

    @Value("${wechatpush.caiHongPiKey}")
    public void setCaiHongPiKey(String value) {
        caiHongPiKey = value;
    }

    public static String getCaiHongPi(){
        String url = "http://api.tianapi.com/caihongpi/index?key=";
        String str = "阳光落在屋里，爱你藏在心里";
        try {
            JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getUrl(url+caiHongPiKey));
            if (jsonObject.getIntValue("code") == 200) {
                str = jsonObject.getJSONArray("newslist").getJSONObject(0).getString("content");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static List<String> strSplit(String input){
        List<String> parts = new ArrayList<>();

        if (input.length() <= 20) {
            parts.add(input);
        } else {
            int length = input.length();
            for (int start = 0; start < length; start += 20) {
                int end = Math.min(length, start + 20);
                parts.add(input.substring(start, end));
            }
        }
        return parts;
    }

}
